package com.atguigu.utils;

import com.atguigu.pojo.*;
import com.atguigu.service.*;
import com.atguigu.service.impl.*;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang3.StringUtils;
import org.junit.Test;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.time.LocalDateTime;
import java.util.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;

//这个文件是用来吧requset的内容打入得到对象中,进行mysql操作的简便实现方式
public class WebUtils {
    //public static void CopyParamToBean(Object bean, HttpServletRequest req){//这是贯口,把request的参数灌到bean中去,自动的
    public static <T> T CopyParamToBean(T bean, Map map){
        try {
            //BeanUtils.populate(bean,req.getParameterMap());//利用BeanUtils.populate把参数放入user中,从而不用从request中提出参数,在把参数放入user中了
            //BeanUtils.populate 利用的bean对象的set方法
            //引申一下,req.getParameterMap()可以换成任何map. 只要key在bean中有对象的set方法,就能用BeanUtils.populate,往bean类的对象中打入vlaue
            //为了以后都可以用这个类,所以我要把req.getParameterMap()换成Map map,并把引用这个类的地方传值时改成req.getParameterMap()
            BeanUtils.populate(bean,map);
            //System.out.println(bean);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        return bean;//既然返回值类型是Object,那么如论如何都要有那么一个返回值,不能没有.
    }

    //获取当前日志输出的文件和行号
    public static String getFileName() {
        return Thread.currentThread().getStackTrace()[1].getFileName()+Thread.currentThread().getStackTrace()[1].getLineNumber()+":";
    }

    //把string类型的字符转换成数字型
    public static Integer parseInt(String strInt,int defaultValue) {
        try {
            return Integer.parseInt(strInt);
        }catch (Exception e){
            //e.printStackTrace();
        }
        return defaultValue;
    }

    //把字符串类型的字符2021-03-27,转成Date类型
    public static Date parseStringToDate(String s){
        Date date=new Date();
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        try {
            date=sdf.parse(s); //从字符串获得Date类对象
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    public static String id(){
        return  System.currentTimeMillis()+"";
        //return  System.currentTimeMillis()+""+(int)(Math.random()*100);//随机数 1611805551563:38

        //String orderId=System.currentTimeMillis()+""+userId+(int)(Math.random()*100);//随机数 1611805551563:1:38
        //一般情况下同一个用户同一时间不会点击2次 在加上随机10-90的整数
    }

    public  static String[] arrayStringSort(String[] arr){
        for(int i=0;i<arr.length-1;i++){
            for(int j=0;j<arr.length-i-1;j++){
                //System.out.println("这是arrayStringSort的提示,正在排序,这是本次的对比结果:"+arr[j].compareTo(arr[j + 1])+"A是:"+arr[j]+",B是:"+arr[j + 1]);
                String a=arr[j];
                String b=arr[j+1];
                if(arr[j].contains(":")){
                    a=arr[j].split(":")[0];
                    b=arr[j+1].split(":")[0];
                }
                if(Integer.parseInt(a)-Integer.parseInt(b)>0){
                    //System.out.println("这是arrayStringSort的提示,正在排序,交换:"+arr[j]+" 和 "+arr[j + 1]);
                    //System.out.println("此时对比结果是:"+arr[j].compareTo(arr[j+1]));
                    String tmp2=arr[j];
                    arr[j]=arr[j+1];
                    arr[j+1]=tmp2;//完成前后移动一次
                }
            }//完成一位往后移动arr.length-i-1次,即移到了能移的最右边
        }//完成arr.length-1次观察
        for (String s:arr){
            //System.out.println("这是arrayStringSort的提示,经过排序后,字符串型数组的顺序是:"+s);
        }
        return arr;
    }
    //在端口号字符串后追加一个port,去重,排序
    public static String addPortsItem(String oldString,String newString){
        String allString=oldString+","+newString;//组合成全一点的字符串
        String[] allStringArray=allString.split(",");//把这个字符串分割成数组

        List<String> allStringList=Arrays.asList(allStringArray);//把数组转成list
        ArrayList<String> arrayList=new ArrayList<>(allStringList);//由于转成的list有坑,再把list转成ArrayList

        Set<String> tmpSet=new HashSet();//生成set,set不允许重复,因此插不进去的就是重复的
        List<String> newList=new ArrayList();//准备接收不重复的item的新ArrayList
        for (String i: allStringList      ) {
            if(tmpSet.add(i)){//判断是否可以加入set,从而看出是否重复
                if(!i.equals("")){
                    newList.add(i);//不重复的字符串插入
                }
            }
        }
        String[] newArray=(String[])newList.toArray(new String[0]);//字符串型的ArrayList转成字符串型的数组
        newArray=arrayStringSort(newArray);//对数组进行排序
        for (String s:newList){
            //System.out.println("这是 addStringItem 的提示,经过排序,顺序为:"+s);
        }

        String newAllString="";//新要组合成的字符串
        for (String i: newArray      ) {
            newAllString=newAllString+","+i;
        }

        newAllString= StringUtils.strip(newAllString,",");//去掉开头的逗号
        //System.out.println("这是 addStringItem 的提示,增加端口后,去重复后的结果:"+newAllString);
        return newAllString;
    }

    public static String reducePortItem(String oldString,String partString){
        String[] arrayPorts=oldString.split(",");//老字符串
        String[] arrayPartString=partString.split(",");//要消减的字符串组
        String newString="";
        List<String> arrayPortList=Arrays.asList(arrayPorts);//老字符串组
        List<String> arrayList=new ArrayList<>(arrayPortList);//老字符串组,这一步是必须滴,因为Arrays.asList(arrayPorts)返回的不支持ArrayLsit的方法,必须构造一个真正的ArrayList对象
        for (String port:arrayPartString) {//要消减的字符串组
            if(arrayList.contains(port)){//如果在老字符串组中能找到要消减组中的一员,就从老字符串组中删除这个对象

                arrayList.remove(port);
            }
        }//消减
        for (String port:arrayList) {//将array转换成字符串
            newString=newString+","+port;
        }
        newString= StringUtils.strip(newString,",");
        //System.out.println("这是 reduceStringItem 提示,消减完的字符串为:"+newString);
        return newString;
    }
    public static String getFreePorts(String usedPorts){
        if(usedPorts.equals("")){
            return "10000,10001,10002,10003,10004,10005";
        }
        //System.out.println("工具中的usePorts不为空");
        String[] usedPortsArray=usedPorts.split(",");
        List<String> usedPortsList=Arrays.asList(usedPortsArray);
        ArrayList<String> usedPortsArrayList=new ArrayList<>(usedPortsList);
        ArrayList<Integer> usedPortsArrayListInteger=new ArrayList<>();
        for(String usedPort:usedPortsArrayList){
            Integer usedPortInteger=Integer.parseInt(usedPort);
            usedPortsArrayListInteger.add(usedPortInteger);
        }
        Integer max=0;
        for(Integer item:usedPortsArrayListInteger){
            if(item>max){
                max=item;
            }
        }
        if(max<10000){
            max=10000;
        }
        Integer spareMax=max+6;
        Integer spareMin=10000;
        ArrayList<Integer> spareList=new ArrayList<>();
        for(Integer item=0;item<spareMax-spareMin;item++){
            spareList.add(spareMin+item);
        }
        ArrayList<Integer> freePortList=new ArrayList<>();
        String freePorts="";
        for(Integer item:spareList){
            //System.out.println(item);
            //System.out.println(usedPortsArrayList);
            if(usedPortsArrayList.contains(""+item)){

            }else{
                //System.out.println("item:"+item+"不在"+usedPortsArrayList+"中");
                freePortList.add(item);
                if(freePorts==""){
                    freePorts=""+item;
                }else {
                    freePorts=freePorts+","+item;
                }
                if(freePortList.size()>=6){
                    break;
                }
            }
        }
        //System.out.println(freePorts);
        return freePorts;
    }

    //写一个返回服务默认端口的方法
    public static String getDefaultPort(String serviceName){
        if(serviceName.contains("nginx")){
            return "80,443";
        }
        if(serviceName.contains("mysql")){
            return "3306";
        }
        if(serviceName.contains("redis")){
            return "6379";
        }
        return "";
    }
    //路径补斜线方法
    public static String addSeparatorChar(String path){
        if(path.charAt(path.length() - 1) != File.separatorChar){//路径分隔符
            return  path+"/";
        }else{
            return path;
        }
    }
    //从字符串中拿文件名
    public static String getFileName(String path){
        String fileDir="",fileName="";
        if(path.contains("/")){
            fileDir=path.substring(0,path.lastIndexOf("/"));
            fileName=path.substring(path.lastIndexOf("/")+1);
        }else {
            fileDir="";
            fileName=path;
        }
        return fileName;
    }
    public static void deleteNginxServiceConfigProxy(NginxServiceConfigProxy nginxServiceConfigProxy){//这是删除一个 nginx proxy记录的过程,它要被单独利用或被删除上级service利用,或被删除server利用
        ServerService serverService=new ServerServiceImpl();
        ServiceService serviceService=new ServiceServiceImpl();
        NginxServiceConfigProxyService nginxServiceConfigProxyService=new NginxServiceConfigProxyServiceImpl();
        NginxServiceConfigPhpService nginxServiceConfigPhpService=new NginxServiceConfigPhpServiceImpl();

        //释放配置实例前,要释放端口号
        String fontPort=nginxServiceConfigProxy.getFrontPort();
        //用现有端口号,查有这个端口号的配置项有几条,如果多于1条,那么就不能为service释放这个端口号;如果只有1条,那么就要从service中删除这个记录,并从server中删除这个port记录
        List<NginxServiceConfigProxy> nginxServiceConfigProxies=nginxServiceConfigProxyService.queryNginxServiceConfigProxiesByFrontPort(fontPort);
        List<NginxServiceConfigPhp> nginxServiceConfigPhps=nginxServiceConfigPhpService.queryNginxServiceConfigPhpsByPort(fontPort);
        if(nginxServiceConfigProxies.size()>1 && nginxServiceConfigPhps.size()>0){
            //System.out.println("81端口不止这一个代理记录再用,还有其他的再用,不能清理service的端口记录");
        }else{
            //System.out.println("81端口,只有这一个代理记录再用了,可以清理了");
            if(fontPort.equals("80")||fontPort.equals("443")){
                //System.out.println("80|443端口是nginx的默认端口,请不要放弃它");
            }else {
                //清理service对81的占用
                Service service=serviceService.queryServiceById(nginxServiceConfigProxy.getServiceId());
                Server server=serverService.queryServerById(nginxServiceConfigProxy.getServerId());
                String servicePorts=WebUtils.reducePortItem(service.getPorts(),fontPort);
                String serverPorts=WebUtils.reducePortItem(server.getPorts(),fontPort);
                service.setPorts(servicePorts);
                server.setPorts(serverPorts);
                serviceService.updateService(service);
                serverService.updateServer(server);
            }
        }
        nginxServiceConfigProxyService.deleteNginxServiceConfigProxyById(nginxServiceConfigProxy.getId());
    }
    public static void deleteNginxServiceConfigPhp(NginxServiceConfigPhp nginxServiceConfigPhp){//这是删除一个 nginx proxy记录的过程,它要被单独利用或被删除上级service利用,或被删除server利用
        ServerService serverService=new ServerServiceImpl();
        ServiceService serviceService=new ServiceServiceImpl();
        NginxServiceConfigProxyService nginxServiceConfigProxyService=new NginxServiceConfigProxyServiceImpl();
        NginxServiceConfigPhpService nginxServiceConfigPhpService=new NginxServiceConfigPhpServiceImpl();

        //释放配置实例前,要释放端口号
        String port=nginxServiceConfigPhp.getPort();
        //用现有端口号,查有这个端口号的配置项有几条,如果多于1条,那么就不能为service释放这个端口号;如果只有1条,那么就要从service中删除这个记录,并从server中删除这个port记录
        List<NginxServiceConfigPhp> nginxServiceConfigPhps=nginxServiceConfigPhpService.queryNginxServiceConfigPhpsByPort(port);
        List<NginxServiceConfigProxy> nginxServiceConfigProxies=nginxServiceConfigProxyService.queryNginxServiceConfigProxiesByFrontPort(port);

        if(nginxServiceConfigPhps.size()>1 && nginxServiceConfigProxies.size()>0){
            //System.out.println("81端口不止这一个代理记录再用,还有其他的再用,不能清理service的端口记录");
        }else{
            //System.out.println("81端口,只有这一个代理记录再用了,可以清理了");
            if(port.equals("80")||port.equals("443")){
                //System.out.println("80|443端口是nginx的默认端口,请不要放弃它");
            }else {
                //清理service对81的占用
                Service service=serviceService.queryServiceById(nginxServiceConfigPhp.getServiceId());
                Server server=serverService.queryServerById(nginxServiceConfigPhp.getServerId());
                String servicePorts=WebUtils.reducePortItem(service.getPorts(),port);
                String serverPorts=WebUtils.reducePortItem(server.getPorts(),port);
                service.setPorts(servicePorts);
                server.setPorts(serverPorts);
                serviceService.updateService(service);
                serverService.updateServer(server);
            }
        }
        nginxServiceConfigPhpService.deleteNginxServiceConfigPhpById(nginxServiceConfigPhp.getId());
    }
    public static void deleteNginxServiceConfigTcpProxy(NginxServiceConfigTcpProxy nginxServiceConfigTcpProxy){//这是删除一个 nginx proxy记录的过程,它要被单独利用或被删除上级service利用,或被删除server利用
        ServerService serverService=new ServerServiceImpl();
        ServiceService serviceService=new ServiceServiceImpl();
        NginxServiceConfigTcpProxyService nginxServiceConfigTcpProxyService=new NginxServiceConfigTcpProxyServiceImpl();

        //释放配置实例前,要释放端口号
        String fontPort=nginxServiceConfigTcpProxy.getFrontPort();
        //tcp 代理的端口不会复用,因此不必查是否有公用的情况,直接从service中清理即可;这一点跟proxy 和 php类型的配置项有所不同
        //清理service对3306的占用
        Service service=serviceService.queryServiceById(nginxServiceConfigTcpProxy.getServiceId());
        Server server=serverService.queryServerById(nginxServiceConfigTcpProxy.getServerId());
        String servicePorts=WebUtils.reducePortItem(service.getPorts(),fontPort);
        String serverPorts=WebUtils.reducePortItem(server.getPorts(),fontPort);
        service.setPorts(servicePorts);
        server.setPorts(serverPorts);
        serviceService.updateService(service);
        serverService.updateServer(server);

        nginxServiceConfigTcpProxyService.deleteNginxServiceConfigTcpProxyById(nginxServiceConfigTcpProxy.getId());
    }
    public static void deleteRedisServiceConfig(RedisServiceConfig redisServiceConfig){//这是删除一个 redis 记录的过程,它要被单独利用或被删除上级service利用,或被删除server利用
        ServerService serverService=new ServerServiceImpl();
        ServiceService serviceService=new ServiceServiceImpl();
        RedisServiceConfigService redisServiceConfigService=new RedisServiceConfigServiceImpl();
        //释放配置实例前,要释放端口号
        String port=redisServiceConfig.getPort();
        //redis的端口不会复用,因此不必查是否有公用的情况,直接从service中清理即可;这一点跟proxy 和 php类型的配置项有所不同
        //清理service对 6379 的占用
        Service service=serviceService.queryServiceById(redisServiceConfig.getServiceId());
        Server server=serverService.queryServerById(redisServiceConfig.getServerId());
        String servicePorts=WebUtils.reducePortItem(service.getPorts(),port);
        String serverPorts=WebUtils.reducePortItem(server.getPorts(),port);
        service.setPorts(servicePorts);
        server.setPorts(serverPorts);
        serviceService.updateService(service);
        serverService.updateServer(server);

        redisServiceConfigService.deleteRedisServiceConfigById(redisServiceConfig.getRedisServiceConfigId());
    }
    public static void deleteMysqlServiceConfig(MysqlServiceConfig mysqlServiceConfig){//这是删除一个 mysql 记录的过程,它要被单独利用或被删除上级service利用,或被删除server利用
        ServerService serverService=new ServerServiceImpl();
        ServiceService serviceService=new ServiceServiceImpl();
        MysqlServiceConfigService mysqlServiceConfigService=new MysqlServiceConfigServiceImpl();
        //释放配置实例前,要释放端口号
        String port=mysqlServiceConfig.getPort();
        //mysql 的端口不会复用,因此不必查是否有公用的情况,直接从service中清理即可;这一点跟proxy 和 php类型的配置项有所不同
        //清理service对 3306 的占用
        Service service=serviceService.queryServiceById(mysqlServiceConfig.getServiceId());
        Server server=serverService.queryServerById(mysqlServiceConfig.getServerId());
        String servicePorts=WebUtils.reducePortItem(service.getPorts(),port);
        String serverPorts=WebUtils.reducePortItem(server.getPorts(),port);
        service.setPorts(servicePorts);
        server.setPorts(serverPorts);
        serviceService.updateService(service);
        serverService.updateServer(server);

        mysqlServiceConfigService.deleteMysqlServiceConfigById(mysqlServiceConfig.getId());
    }
    public static void deleteMongoServiceConfig(MongoServiceConfig mongoServiceConfig){//这是删除一个 mongo 记录的过程,它要被单独利用或被删除上级service利用,或被删除server利用
        ServerService serverService=new ServerServiceImpl();
        ServiceService serviceService=new ServiceServiceImpl();
        MongoServiceConfigService mongoServiceConfigService=new MongoServiceConfigServiceImpl();
        //释放配置实例前,要释放端口号
        String port=mongoServiceConfig.getPort();
        //mongo 的端口不会复用,因此不必查是否有公用的情况,直接从service中清理即可;这一点跟proxy 和 php类型的配置项有所不同
        //清理service对 27017 的占用
        Service service=serviceService.queryServiceById(mongoServiceConfig.getServiceId());
        Server server=serverService.queryServerById(mongoServiceConfig.getServerId());
        String servicePorts=WebUtils.reducePortItem(service.getPorts(),port);
        String serverPorts=WebUtils.reducePortItem(server.getPorts(),port);
        service.setPorts(servicePorts);
        server.setPorts(serverPorts);
        serviceService.updateService(service);
        serverService.updateServer(server);

        mongoServiceConfigService.deleteMongoServiceConfigById(mongoServiceConfig.getId());
    }
    public static void deleteDockerServiceConfig(DockerServiceConfig dockerServiceConfig){//这是删除一个 docker 记录的过程,它要被单独利用或被删除上级service利用,或被删除server利用
        DockerServiceConfigService dockerServiceConfigService=new DockerServiceConfigServiceImpl();
        //该配置文件中没有声明端口号,不需要清除 service和 server的端口号
        dockerServiceConfigService.deleteDockerServiceConfigById(dockerServiceConfig.getId());
    }
    public static void deleteTomcatServiceConfig(TomcatServiceConfig tomcatServiceConfig){//这是删除一个 tomcat 记录的过程,它要被单独利用或被删除上级service利用,或被删除server利用
        TomcatServiceConfigService tomcatServiceConfigService=new TomcatServiceConfigServiceImpl();
        //该配置文件中有声明端口号,需要清除 service和 server的端口号
        ServerService serverService=new ServerServiceImpl();
        ServiceService serviceService=new ServiceServiceImpl();
        String httpPort=tomcatServiceConfig.getHttpPort();
        //mongo 的端口不会复用,因此不必查是否有公用的情况,直接从service中清理即可;这一点跟proxy 和 php类型的配置项有所不同
        //清理service对 27017 的占用
        Service service=serviceService.queryServiceById(tomcatServiceConfig.getServiceId());
        Server server=serverService.queryServerById(tomcatServiceConfig.getServerId());
        String servicePorts=WebUtils.reducePortItem(service.getPorts(),httpPort);
        String serverPorts=WebUtils.reducePortItem(server.getPorts(),httpPort);

        String shutdownPort=tomcatServiceConfig.getShutdownPort();
        servicePorts=WebUtils.reducePortItem(servicePorts,shutdownPort);
        serverPorts=WebUtils.reducePortItem(serverPorts,shutdownPort);

        service.setPorts(servicePorts);
        server.setPorts(serverPorts);
        serviceService.updateService(service);
        serverService.updateServer(server);

        tomcatServiceConfigService.deleteTomcatServiceConfigById(tomcatServiceConfig.getId());
    }
    public static void deleteTomcatAndHeartbeatServiceConfig(TomcatAndHeartbeatServiceConfig tomcatAndHeartbeatServiceConfig){//这是删除一个 tomcatAndHeartbeat 记录的过程,它要被单独利用或被删除上级service利用,或被删除server利用
        TomcatAndHeartbeatServiceConfigService tomcatAndHeartbeatServiceConfigService=new TomcatAndHeartbeatServiceConfigServiceImpl();
        //该配置文件中有声明端口号,需要清除 service和 server的端口号
        ServerService serverService=new ServerServiceImpl();
        ServiceService serviceService=new ServiceServiceImpl();
        String httpPort=tomcatAndHeartbeatServiceConfig.getHttpPort();
        //mongo 的端口不会复用,因此不必查是否有公用的情况,直接从service中清理即可;这一点跟proxy 和 php类型的配置项有所不同
        //清理service对 27017 的占用
        Service service=serviceService.queryServiceById(tomcatAndHeartbeatServiceConfig.getServiceId());
        Server server=serverService.queryServerById(tomcatAndHeartbeatServiceConfig.getServerId());
        String servicePorts=WebUtils.reducePortItem(service.getPorts(),httpPort);
        String serverPorts=WebUtils.reducePortItem(server.getPorts(),httpPort);

        String shutdownPort=tomcatAndHeartbeatServiceConfig.getShutdownPort();
        servicePorts=WebUtils.reducePortItem(servicePorts,shutdownPort);
        serverPorts=WebUtils.reducePortItem(serverPorts,shutdownPort);

        service.setPorts(servicePorts);
        server.setPorts(serverPorts);
        serviceService.updateService(service);
        serverService.updateServer(server);
        tomcatAndHeartbeatServiceConfigService.deleteTomcatAndHeartbeatServiceConfigById(tomcatAndHeartbeatServiceConfig.getId());
    }

    public static void deleteService(Service service){
        ServerService serverService=new ServerServiceImpl();
        ServiceService serviceService=new ServiceServiceImpl();
        NginxServiceConfigProxyService nginxServiceConfigProxyService=new NginxServiceConfigProxyServiceImpl();
        NginxServiceConfigPhpService nginxServiceConfigPhpService=new NginxServiceConfigPhpServiceImpl();
        NginxServiceConfigTcpProxyService nginxServiceConfigTcpProxyService=new NginxServiceConfigTcpProxyServiceImpl();
        RedisServiceConfigService redisServiceConfigService=new RedisServiceConfigServiceImpl();
        MysqlServiceConfigService mysqlServiceConfigService=new MysqlServiceConfigServiceImpl();
        MongoServiceConfigService mongoServiceConfigService=new MongoServiceConfigServiceImpl();
        DockerServiceConfigService dockerServiceConfigService=new DockerServiceConfigServiceImpl();
        TomcatServiceConfigService tomcatServiceConfigService=new TomcatServiceConfigServiceImpl();
        TomcatAndHeartbeatServiceConfigService tomcatAndHeartbeatServiceConfigService=new TomcatAndHeartbeatServiceConfigServiceImpl();

        if(service.getName().contains("nginx")){//这是为nginxproxy准备的删除项,在删除service前要先删除配置项
            List<NginxServiceConfigProxy> nginxServiceConfigProxies=nginxServiceConfigProxyService.queryNginxServiceConfigProxiesByServiceId(service.getId());
            for (NginxServiceConfigProxy nginxServiceConfigProxy:nginxServiceConfigProxies) {
                WebUtils.deleteNginxServiceConfigProxy(nginxServiceConfigProxy);//这个方法中包含有递归删除port占用的方法
                //nginxServiceConfigProxyService.deleteNginxServiceConfigProxyById(nginxServiceConfigProxy.getId());
            }
            List<NginxServiceConfigPhp> nginxServiceConfigPhps=nginxServiceConfigPhpService.queryNginxServiceConfigPhpsByServiceId(service.getId());
            for (NginxServiceConfigPhp nginxServiceConfigPhp:nginxServiceConfigPhps) {
                WebUtils.deleteNginxServiceConfigPhp(nginxServiceConfigPhp);
            }
            List<NginxServiceConfigTcpProxy> nginxServiceConfigTcpProxies=nginxServiceConfigTcpProxyService.queryNginxServiceConfigTcpProxiesByServiceId(service.getId());
            for (NginxServiceConfigTcpProxy nginxServiceConfigTcpProxy:nginxServiceConfigTcpProxies) {
                WebUtils.deleteNginxServiceConfigTcpProxy(nginxServiceConfigTcpProxy);//这个方法中包含有递归删除port占用的方法
            }
        }else
            if(service.getName().contains("redis")){
            RedisServiceConfig redisServiceConfig=redisServiceConfigService.queryRedisServiceConfigByServiceId(service.getId());
            redisServiceConfigService.deleteRedisServiceConfigById(redisServiceConfig.getRedisServiceConfigId());
        }else
            if(service.getName().contains("mysql")){
            MysqlServiceConfig mysqlServiceConfig=mysqlServiceConfigService.queryMysqlServiceConfigByServiceId(service.getId());
            mysqlServiceConfigService.deleteMysqlServiceConfigById(mysqlServiceConfig.getId());
        }else
            if(service.getName().contains("mongo")){
            MongoServiceConfig mongoServiceConfig=mongoServiceConfigService.queryMongoServiceConfigByServiceId(service.getId());
            mongoServiceConfigService.deleteMongoServiceConfigById(mongoServiceConfig.getId());
        }else
            if(service.getName().contains("docker")){
            DockerServiceConfig dockerServiceConfig=dockerServiceConfigService.queryDockerServiceConfigByServiceId(service.getId());
            dockerServiceConfigService.deleteDockerServiceConfigById(dockerServiceConfig.getId());
        }else
            if(service.getName().contains("tomcatAndHeartbeat")){
            TomcatAndHeartbeatServiceConfig tomcatAndHeartbeatServiceConfig=tomcatAndHeartbeatServiceConfigService.queryTomcatAndHeartbeatServiceConfigByServiceId(service.getId());
            tomcatAndHeartbeatServiceConfigService.deleteTomcatAndHeartbeatServiceConfigById(tomcatAndHeartbeatServiceConfig.getId());
        }else
            if(service.getName().contains("tomcat")){
            TomcatServiceConfig tomcatServiceConfig=tomcatServiceConfigService.queryTomcatServiceConfigByServiceId(service.getId());
            tomcatServiceConfigService.deleteTomcatServiceConfigById(tomcatServiceConfig.getId());
        }

        Server server=serverService.queryServerById(service.getServerId());
        String serverPort=WebUtils.reducePortItem(server.getPorts(),WebUtils.getDefaultPort(service.getName()));
        server.setPorts(serverPort);
        serverService.updateServer(server);//这样就完成了"删除服务,随机删除主机上的端口占用"

        serviceService.deleteServiceById(service.getId());
    }
    public static void deleteServer(Server server){
        ServerService serverService=new ServerServiceImpl();
        ServiceService serviceService=new ServiceServiceImpl();
        List<Service> services=serviceService.queryServicesByServerId(server.getId());
        for (Service service:services) {
            WebUtils.deleteService(service);
        }
        serverService.deleteServerById(server.getId());
    }

    public static List<TomcatAndHeartbeatServiceConfig> getTomcatAndHeartbeatServiceConfigsByProjectModuleId(String projectModuleId){
        TomcatAndHeartbeatServiceConfigService tomcatAndHeartbeatServiceConfigService =new TomcatAndHeartbeatServiceConfigServiceImpl();
        return tomcatAndHeartbeatServiceConfigService.queryTomcatAndHeartbeatServiceConfigsByProjectModuleId(projectModuleId);
    }
    public static List<TomcatServiceConfig> getTomcatServiceConfigsByProjectModuleId(String projectModuleId){
        TomcatServiceConfigService tomcatServiceConfigService =new TomcatServiceConfigServiceImpl();
        return tomcatServiceConfigService.queryTomcatServiceConfigsByProjectModuleId(projectModuleId);
    }
}
